`timescale 1ns / 1ps

module blinker(
    input clk_i,  // 1khz
    input [2:0] blink_ctrl_i,

    output reg blink_pulse_o = 'b1
    );
    reg [9:0] blink_period;
    wire [9:0] half_blink_period;
    reg [9:0] timestamp;
    
    assign half_blink_period = {1'b0, blink_period[9:1]};
    
    initial begin
        timestamp <= 'b0;
        blink_period <= 'b0;
    end
    
    // update blink period according to the control signal
    always @(blink_ctrl_i) begin
        case (blink_ctrl_i)
            3'b100: blink_period <= 10'd250;   // 4Hz
            3'b010: blink_period <= 10'd500;   // 2Hz
            3'b001: blink_period <= 10'd1000;  // 1Hz
            default: blink_period <= 10'd0;    // no blink
        endcase
    end

    // generates the blink signal according to blink period
    always @(posedge clk_i) begin
        timestamp <= (timestamp>=blink_period) ? 10'd1 : timestamp+'d1;
        if (timestamp > half_blink_period) begin
            blink_pulse_o <= 'b1;
        end else begin
            blink_pulse_o <= 'b0;
        end
    end
endmodule
